import { http } from "@/utils/http";
// 比赛相关的api
type Result = {
  code: number;
  message: string;
  data?: any;
  timestamp: string;
};
// 比赛数据
type MatchData = {
  id: number;
  matchTime: string;
  leagueName: string;
  homeTeam: string;
  awayTeam: string;
  homeTeamLogo: string | null;
  awayTeamLogo: string | null;
  externalId: string;
  homeWinRate?: number; // 主队胜率
  awayWinRate?: number; // 客队胜率
  boldRecommendation?: "recommend_bold" | "recommend_stable" | "none"; // 胆推荐类型
  createdAt: string;
  updatedAt: string;
  bettings: BettingData[];
};
// 投注数据
type BettingData = {
  id: number;
  matchId: number;
  jingcaiNumber: string | null;
  beidanNumber: string;
  jingcaiSingle: string | null;
  jingcaiHandicap: string | null;
  beidanHandicap: string;
  winOdds: string;
  drawOdds: string | null;
  loseOdds: string;
  recommendedResult?: string[];
  createdAt: string;
  updatedAt: string;
  awayScore?: number; // 客队比分
  homeScore?: number; // 主队比分
  matchResult?: string; // 比赛结果
  predictionHit?: boolean; // 预测命中
};
// 比赛列表
type MatchListResponse = {
  data: MatchData[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
};
// 比赛列表参数
type MatchListParams = {
  page?: number;
  pageSize?: number;
  dateFilter?: "current" | "historical"; // 数据筛选类型：当前数据或往期数据
  startTime?: string; // 开始时间
  endTime?: string; // 结束时间
};

/** 获取比赛列表 */
export const getMatchList = (data?: MatchListParams) => {
  return http.request<Result & { data: MatchListResponse }>(
    "get",
    "/api/matches",
    { params: data }
  );
};

/** 导入比赛数据 */
export const importMatches = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return http.request<Result>("post", "/api/matches/import", {
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

/** 导入比分数据 */
export const importScores = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return http.request<Result>("post", "/api/matches/import-score", {
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

/** 删除比赛 */
export const deleteMatch = (id: number) => {
  return http.request<Result>("delete", `/api/matches/${id}`);
};

/** 更新比赛 */
export const updateMatch = (id: number, data: Partial<MatchData>) => {
  return http.request<Result>("patch", `/api/matches/${id}`, { data });
};

/** 投注推荐结果 */
type BettingRecommendation = {
  id: number;
  recommendedResult: string[];
};

/** 提交投注推荐 */
export const submitBettingRecommendation = (data: BettingRecommendation) => {
  return http.request<Result>("patch", `/api/betting/${data.id}/recommended`, {
    data
  });
};

export type {
  MatchData,
  BettingData,
  MatchListResponse,
  MatchListParams,
  BettingRecommendation
};
